/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.si3.controlador;

import java.util.HashMap;
import java.util.Map;

import com.si3.modelo.Alumno;
import com.si3.servicio.AddAlumnoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/** 
 *
 * @author fjavieralba
 */
public class AddAlumnoController extends SimpleFormController
{
    private AddAlumnoService addAlumnoService;

    public void setAddAlumnoService(AddAlumnoService addUserService)
    {
        this.addAlumnoService = addUserService;
    }

    public AddAlumnoService getAddAlumnoService()
    {
        return addAlumnoService;
    }
    
    
    public AddAlumnoController()
    {
        setCommandClass(Alumno.class);
        setCommandName("alumno");
        setSuccessView("startalumno");
        setFormView("newAlumnoForm");
    }
    

    @Override
   protected void initBinder(HttpServletRequest request,
            ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(com.si3.modelo.Centro.class, new CentroEditorSupport());
        System.out.println("\t*** registered Centro property editor.");
    }
    
    protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception 
    {
    	Map referenceData = new HashMap();
    	referenceData.put("centros", addAlumnoService.getAllCentros());
    	return referenceData;
      } 
    
    @Override
    protected ModelAndView onSubmit(HttpServletRequest request, 
                                    HttpServletResponse response, 
                                    Object command, 
                                    BindException errors) throws Exception 
    {
        ModelAndView mv = new ModelAndView(getSuccessView());
        
        Alumno alumno = (Alumno) command;
        addAlumnoService.addNewAlumno(alumno);
        
        return mv;
    }
    
}
